var perfix=$("#perfix").val();
$(function () {
    $('#summernote').summernote({
        height: 200,
        tabsize: 2,
        lang: 'zh-CN'
    });
});
$(function () {
    $("#newsGet").click();
    $("#b").fadeIn(500)
});
function newsGet() {
    var curpage=$("#page").val();
    $.ajax({
        url: perfix+"/port/newsGet",
        type: "POST",
        data: {
            page: curpage
        },
        success: function (data) {
            if (data.code==200){
                var news = data.data;
                $("#details").html("");
                var m=0;
                for(var i=0;i<news.length;i++){
                    $("#details").append("<tr><td><button style='font-size: 14px;line-height: 0.8' class='btn btn-danger btn-sm' onclick='deleteNew("+news[i].newId+")'>删除新闻</button></td><td onclick='showDetail("+news[i].newId+")'><a>"+news[i].title+"</a></td><td onclick='showDetail("+news[i].newId+")'>"+news[i].time+"</td><td onclick='showDetail("+news[i].newId+")'>"+news[i].languagetype+"</td></tr>");
                    m++;
                };
                while (m<15){
                    m++;
                    $("#details").append("<tr><td></td><td></td><td></td><td></td></tr>");
                }
                //分页
                var countpage=Math.ceil(parseInt(data.count)/parseInt(10));
                $("#pagecount").text(countpage)
                $("#crpage").text(curpage)
                $("#pages").html("")
                var prepage=curpage-1;
                var forwardpage=parseInt(curpage)+1;
                if(curpage==1){
                    $("#pages").append("<button title='1' onclick='changepage(this.title)' class='detailpage'><</button>")
                }else{
                    $("#pages").append("<button title="+prepage+" onclick='changepage(this.title)' class='detailpage'><</button>")
                }
                if(countpage<6){
                    for(var i=1;i<countpage+1;i++){
                        if(i==curpage){
                            $("#pages").append("<button style='background: #44AF24;color: #FFFFFF' onclick='changepage(this.title)' title="+i+" class='detailpage'>"+i+"</button>")
                        }else{
                            $("#pages").append("<button onclick='changepage(this.title)' title="+i+" class='detailpage'>"+i+"</button>")
                        }
                    }
                }else{
                    var dif=countpage-curpage;
                    if (dif<=3) {
                        var d=4-dif;
                        for(var i=curpage-d;i<countpage+1;i++){
                            if(i==curpage){
                                $("#pages").append("<button style='background: #44AF24;color: #FFFFFF' onclick='changepage(this.title)' title="+i+" class='detailpage'>"+i+"</button>")
                            }else{
                                $("#pages").append("<button onclick='changepage(this.title)' title="+i+" class='detailpage'>"+i+"</button>")
                            }
                        }
                    } else{
                        var c=parseInt(curpage)+4;
                        if(curpage==1){
                            for(var i=curpage;i<c+1;i++){
                                if(i==curpage){
                                    $("#pages").append("<button style='background: #44AF24;color: #FFFFFF' onclick='changepage(this.title)' title="+i+" class='detailpage'>"+i+"</button>")
                                }else{
                                    $("#pages").append("<button onclick='changepage(this.title)' title="+i+" class='detailpage'>"+i+"</button>")
                                }
                            }
                        }else if(curpage==2){
                            for(var i=curpage-1;i<c;i++){
                                if(i==curpage){
                                    $("#pages").append("<button style='background: #44AF24;color: #FFFFFF' onclick='changepage(this.title)' title="+i+" class='detailpage'>"+i+"</button>")
                                }else{
                                    $("#pages").append("<button onclick='changepage(this.title)' title="+i+" class='detailpage'>"+i+"</button>")
                                }
                            }
                        }else{
                            for(var i=curpage-2;i<c-1;i++){
                                if(i==curpage){
                                    $("#pages").append("<button style='background: #44AF24;color: #FFFFFF' onclick='changepage(this.title)' title="+i+" class='detailpage'>"+i+"</button>")
                                }else{
                                    $("#pages").append("<button onclick='changepage(this.title)' title="+i+" class='detailpage'>"+i+"</button>")
                                }
                            }
                        }
                    }
                }
                if(curpage==countpage){
                    $("#pages").append("<button title="+curpage+" onclick='changepage(this.title)' class='detailpage'>></button>")
                }else{
                    $("#pages").append("<button title="+forwardpage+" onclick='changepage(this.title)' class='detailpage'>></button>")
                }
            } else{
                alert(data.msg)
            }
        }
    })
};
function changepage(page) {
    $("#page").val(page);
    $("#newsGet").click();
}
function addNews() {
    $("#newsId").val("");
    $("#title").val("");
    $("#language").val("")
    $('#summernote').summernote('code',"");
    $("#showMtk").click();
};
function sub() {
    var newsId=$("#newsId").val();
    var title=$("#title").val();
    var language=$("#language").val();
    var summernote = $('#summernote').summernote('code');
    $.ajax({
        url: perfix+"/editor/newsPut",
        type: "POST",
        data: {
            newsId: newsId,
            title: title,
            language: language,
            summernote: summernote
        },
        success: function (data) {
            if (data.code==200){
                alert("提交成功")
                // location.reload()
                $("#newsGet").click()
                $("#mtkClose").click()
            }else{
                alert(data.msg)
            }
        }
    })
};
function showDetail(id) {
    $("#newsId").val(id);
    $.ajax({
        url: perfix+"/port/oneNewGet",
        type: "POST",
        data: {
            newsId: id
        },
        success: function (data) {
            if (data.code==200){
                var news = data.data;
                $("#title").val(news.title);
                $("#language").val(news.languagetype)
                $('#summernote').summernote('code',news.content);
                $("#showMtk").click();
            } else{
                alert(data.msg)
            }
        }
    })
};
function deleteNew(id) {
    $.ajax({
        url: perfix+"/editor/deleteNew",
        type: "POST",
        data: {
            newsId: id
        },
        success: function (data) {
            if (data.code==200){
                alert("删除成功");
                location.reload();
            }
        }
    })
}